package com.ruoyi.project.dataviewSystem.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.ftp.FtpUtil;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.config.ServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.dataviewSystem.domain.SurveillancePicture;
import com.ruoyi.project.dataviewSystem.service.ISurveillancePictureService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 监控图片Controller
 * 
 * @author wangshuo
 * @date 2021-01-31
 */
@RestController
@RequestMapping("/dataviewSystem/surveillancePicture")
public class SurveillancePictureController extends BaseController
{
    @Autowired
    private ISurveillancePictureService surveillancePictureService;

    /**
     * 查询监控图片列表
     */
    @PreAuthorize("@ss.hasPermi('dataviewSystem:surveillancePicture:list')")
    @GetMapping("/list")
    public TableDataInfo list(SurveillancePicture surveillancePicture)
    {
        startPage();
        List<SurveillancePicture> list = surveillancePictureService.selectSurveillancePictureList(surveillancePicture);
        return getDataTable(list);
    }

    /**
     * 导出监控图片列表
     */
    @PreAuthorize("@ss.hasPermi('dataviewSystem:surveillancePicture:export')")
    @Log(title = "监控图片", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SurveillancePicture surveillancePicture)
    {
        List<SurveillancePicture> list = surveillancePictureService.selectSurveillancePictureList(surveillancePicture);
        ExcelUtil<SurveillancePicture> util = new ExcelUtil<SurveillancePicture>(SurveillancePicture.class);
        return util.exportExcel(list, "surveillancePicture");
    }

    /**
     * 获取监控图片详细信息
     */
    @PreAuthorize("@ss.hasPermi('dataviewSystem:surveillancePicture:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(surveillancePictureService.selectSurveillancePictureById(id));
    }

    /**
     * 新增监控图片
     */
    @PreAuthorize("@ss.hasPermi('dataviewSystem:surveillancePicture:add')")
    @Log(title = "监控图片", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SurveillancePicture surveillancePicture)
    {
        return toAjax(surveillancePictureService.insertSurveillancePicture(surveillancePicture));
    }

    /**
     * 修改监控图片
     */
    @PreAuthorize("@ss.hasPermi('dataviewSystem:surveillancePicture:edit')")
    @Log(title = "监控图片", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SurveillancePicture surveillancePicture)
    {
        return toAjax(surveillancePictureService.updateSurveillancePicture(surveillancePicture));
    }

    /**
     * 删除监控图片
     */
    @PreAuthorize("@ss.hasPermi('dataviewSystem:surveillancePicture:remove')")
    @Log(title = "监控图片", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(surveillancePictureService.deleteSurveillancePictureByIds(ids));
    }
//
//    @Value("${ruoyi.surveillancePicPath}")
//    private String surveillancePicPath ="/usr/temper/ftp/uploads";
    @Autowired
    private ServerConfig serverConfig;
    @GetMapping("/getPic")
    public AjaxResult getPicture()
    {
        String remoteAddr = "121.37.31.192";
        String userName = "root";
        String pswd = "We@#Hx87775236@#!!!";
        ArrayList<String> linuxFileList = null;
        try {
            linuxFileList = FtpUtil.getLinuxFileList(RuoYiConfig.getSurveillancePicPath(), remoteAddr, userName, pswd);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<String> collect = linuxFileList.stream().map(x -> {
            String y = "";
            y = serverConfig.getUrl() + RuoYiConfig.getSurveillancePicPath() + "/" + x;
            return y;
        }).collect(Collectors.toList());
        return AjaxResult.success(collect);
    }

}
